﻿using System;
using System.Collections;

namespace ThreadSafeControls {
    partial class ThreadSafeCheckedListBox {
        public class CheckedItemCollection : IList {
            
            private readonly ThreadSafeCheckedListBox _listBox;
            
            internal CheckedItemCollection(ThreadSafeCheckedListBox listBox) {
                if (listBox == null)
                    throw new ArgumentNullException("listBox");
                
                _listBox = listBox;
            }

            public IList Synchronize() {
                return new SynchronizedObjectCollection(this);
            }

            public object this[int index] {
                get { return _listBox.ReturnAsync(c => c.CheckedItems[index]); }
            }

            public int Count {
                get { return _listBox.ReturnAsync(c => c.CheckedItems.Count); }
            }

            public bool Contains(object selectedObject) {
                return _listBox.ReturnAsync(c => c.CheckedItems.Contains(selectedObject));
            }

            public void CopyTo(Array destination, int index) {
                _listBox.UpdateAsync(c => c.CheckedItems.CopyTo(destination, index));
            }

            public int IndexOf(object selectedObject) {
                return _listBox.ReturnAsync(c => c.CheckedItems.IndexOf(selectedObject));
            }

            public IEnumerator GetEnumerator() {
                return _listBox.ReturnAsync(c => c.CheckedItems.GetEnumerator());
            }

            #region Explicit Interface Members

            bool IList.IsFixedSize {
                get { return false; }
            }

            bool IList.IsReadOnly {
                get { return false; }
            }

            bool ICollection.IsSynchronized {
                get { return false; }
            }

            object ICollection.SyncRoot {
                get { return ((ICollection)_listBox.Control.CheckedItems).SyncRoot; }
            }

            int IList.Add(object value) {
                return _listBox.ReturnAsync(c => ((IList)c.CheckedItems).Add(value));
            }

            void IList.Insert(int index, object value) {
                _listBox.UpdateAsync(c => ((IList)c.CheckedItems).Insert(index, value));
            }
            
            void IList.Remove(object value) {
                _listBox.UpdateAsync(c => ((IList)c.CheckedItems).Remove(value));
            }

            void IList.RemoveAt(int index) {
                _listBox.UpdateAsync(c => ((IList)c.CheckedItems).RemoveAt(index));
            }
            
            void IList.Clear() {
                _listBox.UpdateAsync(c => ((IList)c.CheckedItems).Clear());
            }

            object IList.this[int index] {
                get { return _listBox.ReturnAsync(c => ((IList)c.CheckedItems)[index]); }
                set { _listBox.UpdateAsync(c => ((IList)c.CheckedItems)[index] = value); }
            }

            #endregion
        }
    }
}
